//
//  ICQuery.m
//  Iching
//
//  Created by Leonardo Celati on 27/12/09.
//  Copyright 2009 Web & Zen Multimedia All rights reserved.
//

#import "ICQueryViewController.h"

@implementation ICQueryViewController

// generate setter and getter
@synthesize icQueryView;
@synthesize icResponseView;
//@synthesize queryText;
//@synthesize consultantName;
//@synthesize startQueryButton;
//@synthesize stopQueryButton;



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	NSLog(@"view did load...");	
	[super viewDidLoad];
	response = [[NSMutableArray alloc] init];
}

- (void) viewDidAppear:(BOOL)animated{
	NSLog(@"view appeared...");
    [icQueryView becomeFirstResponder];
    [super viewDidAppear:animated];
}

- (void) viewWillDisappear:(BOOL)animated {
	NSLog(@"view disappeared...");
    [icQueryView resignFirstResponder];
    [super viewWillDisappear:animated];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
	NSLog(@"returning text: %s", [theTextField text] );
    //if (theTextField == textField) {
	
	[theTextField resignFirstResponder];
	[icQueryView becomeFirstResponder];
	
    //}
	
    return YES;
}

// detect shaking
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
	//NSLog(@"shake...");
    if ( event.subtype == UIEventSubtypeMotionShake && launched < MAX_LAUNCH){
		// on first tentative we lock all element on view
		if (launched==0) {
			[self lockView];
		}
		
		// start measurement
		start = [NSDate timeIntervalSinceReferenceDate];
		[self resignFirstResponder];
    }
	
	//[self resignFirstResponder];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
	
	//NSLog(@"Shaking...");
    if ( event.subtype == UIEventSubtypeMotionShake && launched < MAX_LAUNCH){

		[self resignFirstResponder];

		// launching coin...		
		[response addObject:[NSNumber numberWithInt:
			[self throwCoins:icQueryView.queryText.text withShakingInterval:([NSDate timeIntervalSinceReferenceDate]-start)]
		]];
		
		[self buildTossView ];
		// increasing...
		launched++;
		
		// if we reach max launch we can build our response
		if (launched==MAX_LAUNCH)[self buildResponseView];
    }
}

// stop running query and reset variable
-(IBAction) stopQuery:(id)sender {
	NSLog(@"stop query...");
	[self unlockView];
}

// lock all element view
-(void)lockView {
	// set variable
	launched=0;
	icQueryView.queryText.enabled = NO;
	icQueryView.consultantName.enabled = NO;
}

// unlock all element view
-(void)unlockView {
	// unlock element and variable
	launched=0;
	//response = nil;
	icQueryView.queryText.enabled = YES;
	icQueryView.consultantName.enabled = YES;
	
	// reset view variable
	
}


// when we shake we toss coins
-(Line)throwCoins:(NSString*)question withShakingInterval:(NSTimeInterval)interval {
	//NSLog(@"%s - %o",question,interval);
	return throwCoins(question,interval);
}

// display the launch
-(void)buildTossView {
	// cleaning existing subview...
	for(UIView *subview in [icQueryView subviews]) {
		if ([subview class] == [ICTossView class]) {
			[subview removeFromSuperview];
			//[subview release];
			break;
		}		
	}	
	
	// building subview
	ICTossView *tossView = [[ICTossView alloc] initWithToss:response];
	tossView.backgroundColor=[UIColor whiteColor];
	[icQueryView addSubview:tossView];
	[tossView release];
} 

// build response view
-(void)buildResponseView {
	// cleaning existing subview...
	for(UIView *subview in [icQueryView subviews]) {
		if ([subview class] == [ICTossView class]) {
			[subview removeFromSuperview];
			//[subview release];
			break;
		}		
	}

	self.view=icResponseView;
	// prepare element on view
	icResponseView.responseLabel.text=icQueryView.queryText.text;
	
}



@end
